﻿using System;
using UnityEngine;
using System.Reflection;
using System.IO;


namespace CustomCommands  // namespace doesn't matter at all
{

    internal class myCommands : MonoBehaviour    // class name doesn't matter at all,  just keep it the same as your filename ->
    {
        
        public bool usingGUI = true;

        public String adminFile = "Unturned_Data/Managed/mods/AdminCommands/UnturnedAdmins.txt";
        

        public void Start()   //gets called once,  at the beginning
        {            
            //Directory.CreateDirectory("Unturned_Data/Managed/mods/MyCommandsFolder");

            ExtraMethods.readAdminsFromFile(adminFile);
 
        }

        public void OnGUI()   //gets called every frame
        {
            if (usingGUI == true)
            {
                GUI.BeginGroup(new Rect(50, 200, 600, 70));
                GUI.Box(new Rect(0, 0, 530, 400), "Custom Commands running!");
                GUI.EndGroup();
            }
        }

        public void Update()   //gets called every frame(-ish)  -  slightly less than OnGUI()
        {
            ExtraMethods.UpdatePlayerList();

            checkForCommands();
        }

        


        public void checkForCommands()
        {
            if (ExtraMethods.getLastMessageText().StartsWith("/"))   //Check last (bottom) message for commands
            {
                String sender = ExtraMethods.getLastMessagePlayerName();
                String command = ExtraMethods.getLastMessageText();
                NetworkChat.sendAlert(""); //avoid looping into commands                

                if (ExtraMethods.isAdmin(sender))
                {
                    //commands for admins

                    if (command.StartsWith("/repeat"))
                    {
                        NetworkChat.sendAlert(command.Substring(8));
                    }
                    else if (command.StartsWith("/killzombies"))
                    {
                        Zombie[] Zombies = UnityEngine.Object.FindObjectsOfType(typeof(Zombie)) as Zombie[];
                        foreach (Zombie Zombie in Zombies)
                        {
                            Zombie.damage(500);
                        }
                        NetworkChat.sendAlert(sender + " has killed " + Zombies.Length + " zombies");
                    }
                }
                

                //commands for non-admins

                if (command.Equals("/online"))
                {
                    ExtraMethods.getNetworkChat().askChat("There are " + ExtraMethods.playerNames.Length + " players online.", 2, ExtraMethods.getNetworkPlayerByPlayerName(sender));
                }

                else if (command.StartsWith("/time"))
                {
                    NetworkChat.sendAlert("Time: " + Sun.getTime());
                    
                }
                


            }   // end of message.startswith("/")
        } //end checkForCommands()




        private void Log(string p)
        {
            System.IO.StreamWriter file = new StreamWriter("Unturned_Data/Managed/mods/CustomCommands_Log.txt", true);
            file.WriteLine(p);
            file.Close();
        }


    } //end class

}  //end namespace